package com.yuanxiao.controller;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import com.yuanxiao.pojo.Order;
import com.yuanxiao.service.OrderService;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;


@Controller
public class OrderController {

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    @Autowired
    private OrderService orderService;
    @Autowired
    private StringRedisTemplate redisTemplate;


    @Value("${order.mq.message.ttl}")
    private String orderMqMessageTtl;

    private String redisKey = "order_remaining";
    private String notPayRedisKey = "order_notpay";


    @RequestMapping("/openbuy")
    @ResponseBody
    public String openbuy(String count) {
        if(ObjectUtil.isEmpty(count)){
            count="0";
            redisTemplate.opsForValue().set(notPayRedisKey, count);
        }
        redisTemplate.opsForValue().set(redisKey, count);
        return "ok";
    }

    @RequestMapping("/")
    public String orderPage(Model model) {
        String remaining = (String)redisTemplate.opsForValue().get(redisKey);
        if (Integer.parseInt(remaining) > 0) {
            model.addAttribute("remaining", "1");
        } else {
            model.addAttribute("remaining", "0");
        }
        return "index";
    }

    @RequestMapping("/buy")
    public String buyGoods(@RequestParam("name") String name, @RequestParam("count") int count, Model model) {

        String availableQuantity = (String)redisTemplate.opsForValue().get(redisKey);
        if (Integer.parseInt(availableQuantity) < 1) {
            String notPayQuantiry = (String)redisTemplate.opsForValue().get(notPayRedisKey);
            if (Integer.parseInt(notPayQuantiry) > 0) {
                model.addAttribute("notPayRedisKey", "1");
            }
            return "failure";
        }

        Order order = new Order();
        order.setId(IdUtil.randomUUID());
        order.setName(name);
        if (count > 1) {
            count = 1;
        }
        order.setCount(count);
        order.setStatus("未付款");
        order.setCancelReason("");
        order.setOrderTime(new Date());
        model.addAttribute("orderId", order.getId());
        rocketMQTemplate.convertAndSend("buy_order", order);
        redisTemplate.opsForValue().decrement(redisKey, 1);
        redisTemplate.opsForValue().increment(notPayRedisKey, 1);
        return "pay";
    }

    @RequestMapping("/pay")
    public String payOrder(@RequestParam("id") String id, Model model) {
        redisTemplate.opsForValue().decrement(notPayRedisKey, 1);
        orderService.updateOrderStatusById(id, "已付款", "");
        return "index";
    }


}
